//
//  Pthread_rwlockViewController.m
//  JKThreadTest
//
//  Created by 王冲 on 2018/10/25.
//  Copyright © 2018年 JK科技有限公司. All rights reserved.
//

#import "Pthread_rwlockViewController.h"
#import <pthread.h>
@interface Pthread_rwlockViewController ()

@property (assign, nonatomic) pthread_rwlock_t lock;

@end

@implementation Pthread_rwlockViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    pthread_rwlock_init(&_lock, NULL);
    
    for (int i = 0; i<10; i++) {
        
        dispatch_async(queue, ^{
            
             [self read];
        });
        
        dispatch_async(queue, ^{
            
            [self write];
        });

    }
}

// 读文件
-(void)read{
    
    // 读可以多条线程进行
    pthread_rwlock_rdlock(&_lock);
    
    sleep(1.0);
    
    NSLog(@"--读--");
    
    pthread_rwlock_unlock(&_lock);
    
}

// 写文件
-(void)write{
    
    // 在写的时候没有读的操作 且 写只能有一条线程进行
    pthread_rwlock_wrlock(&_lock);
    
    sleep(1.0);
    
    NSLog(@"--写--");
    
    pthread_rwlock_unlock(&_lock);
    
}

-(void)dd{
    
    // 初始化锁
    pthread_rwlock_t lock;
    pthread_rwlock_init(&lock, NULL);
    // 读—加锁
    pthread_rwlock_rdlock(&lock);
    // 读-尝试加锁
    pthread_rwlock_tryrdlock(&lock);
    // 写加锁
    pthread_rwlock_wrlock(&lock);
    // 写-尝试加锁
    pthread_rwlock_trywrlock(&lock);
    // 解锁
    pthread_rwlock_unlock(&lock);
    // 销毁锁
    pthread_rwlock_destroy(&lock);
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
